<?php
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $screen->getAjaxLinkUrl('saveMessage', NULL, array(
    'messageId' => $messageId,
    'parentMessageId' => $brandId,
    'postModel' => $modelClass));
$updateId = $screen->getAjaxContainerId();

$messageNameField = 'message_name_' . $messageId;
$priceField = 'media_price_' . $messageId;
$imageField = 'image_media_image_' . $messageId;
$descriptionField = 'message_description_' . $messageId;

$model->addField($messageNameField);
$model->addField($priceField);
$model->addField($imageField);
$model->addField($descriptionField);

if ($messageId > 0) {
    $message = new BWMessage($messageId);

    $model->$messageNameField = $message->name;
    $model->$descriptionField = $message->description;
    $priceMedia = $message->getMediaManager()->getMedia('price');
    if ($priceMedia)
        $model->$priceField = $message->getMediaManager()->getMedia('price')->content;

    $imageMedia = $message->getMediaManager()->getMedia('image');
    if ($imageMedia)
        $model->$imageField = $message->getMediaManager()->getMedia('image')->content;
}
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $messageId;?>() {

//        var formData = new FormData($("#message-form")[0]);
var formElement = document.getElementById("message-form");
        var formData = new FormData(formElement);
        
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").load("<?php echo $screen->getLayoutView(); ?>");
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading－－－－. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<?php
//$model = new BWMessageFormModel();
//if ($messageId > 0) {//for modify
//    $message = new BWMessage($messageId);
//    $model->name = $message->name;
//    $model->description = $message->description;
//    $model->price = $message->getMediaManager()->getMedia('price')->content;
//    $model->image = $message->getMediaManager()->getMedia('image')->content;
//}
?>
<div id="myModal<?php echo $messageId;?>" class="modal hide fade" style="width: 1000px;left: 33%; " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3>添加商品</h3>
</div>
<div class="modal-body" id="Event_Add_Body" style="display:block">

    <table class="table table-bordered table-striped">
        <colgroup>
            <col class="span1">
            <col class="span7">
        </colgroup>
        <thead>
            <tr>
                <th>商品属性</th>
                <th>详细内容</th>
            </tr>
        </thead>
        <tbody>
                <tr><td> <code>商品名称</code> </td>
                    <td><?php //echo CHtml::activeTextArea($model, 'name', array('name' => "messageName" . $messageId, 'placeholder' => '商品名称...', 'required' => true));   ?>
                        <?php echo $form->textArea($model, $messageNameField, array('placeholder' => '商品名称...', 'required' => true)); ?>
                    </td>
                </tr>
                <tr><td><code>商品价格</code> </td>
                    <td><?php //echo CHtml::activeTextField($model, 'price', array('name' => "messagePrice" . $messageId, 'placeholder' => '商品价格...'));   ?>
                        <?php echo $form->textField($model, $priceField, array('placeholder' => '商品价格...')); ?>
                    </td>
                </tr>
                <tr><td><code>商品缩略图</code> </td>
                    <td><!--<input type="file" name="shoppicture0"  id="shoppicture0">-->
                        <?php
                        echo $form->fileField($model, $imageField);
                        if ($messageId > 0) {
                            $imageUrl = Yii::app()->request->baseUrl . $model->$imageField;
                            echo "<img src=" . $imageUrl . " width=50 height=50 />";
                        }
                        ?>
                    </td>
                </tr>
                <tr>
                    <td> <code>商品详细内容</code>  </td>
                    <td><?php //echo CHtml::activeTextArea($model, 'description', array('name' => "messageDesc" . $messageId, 'placeholder' => '商品描述...'));   ?>
                        <?php echo $form->textArea($model, $descriptionField, array('placeholder' => '商品描述...')); ?>
                    </td>
                </tr>
            </tbody>
    </table>
</div> 
<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
    <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:send<?php echo $messageId;?>()">保存</button>
    <?php
    ///echo $control->getAjaxLink(array('name' => '保存', 'id' => 'saveMessage_2'), array('messageId' => $messageId,'MmessageId'=>$MmessageId ,'htmlOptions' => array('class' => 'btn', 'enctype' => 'multipart/form-data')));
    ?>
</div>
</div>